<template>
	<view>
		<uni-forms :rules="rules" :value="formData" ref="form" validate-trigger="bind" err-show-type="undertext">
			<view v-for="(item,index) in Entity" :key="index">
				<view v-if="item.type=='select'" class="uni-list">
					<view class="uni-list-cell">
						<view class="uni-list-cell-left">
							{{item.label}}
						</view>
						<view class="uni-list-cell-db">
							<view v-if="readonly==1" class="uni-input">{{item.request[item.index].Text}}</view>
							<picker v-else @change="bindPickerChange($event,index)" :value="item.index"
								:range="item.request" range-key="Text">
								<view class="uni-input">{{item.request[item.index].Text}}</view>
							</picker>
						</view>
					</view>
				</view>
				<view v-else-if="item.type=='image'" class="uni-list">
					<view class="uni-list-cell">
						<view class="uni-list-cell-left">
							{{item.label}}
						</view>
						<WtmImage :count="item.count!=null?item.count:1"
							:sizeTypeIndex="item.sizeTypeIndex!=null?item.sizeTypeIndex:2"
							:sourceTypeIndex="item.sourceTypeIndex!=null?item.sourceTypeIndex:2" :images="item">
						</WtmImage>
						<label class="color-red"
							v-if="errormsgs[item.name[0]+'.'+item.name[1]]!=null">{{errormsgs[item.name[0]+'.'+item.name[1]]}}</label>
					</view>
				</view>
				<!-- 			<view v-else class="uni-form-item uni-column">
				<view class="title"><text class="uni-form-item__title">{{item.label}}</text></view>
				<view v-if="readonly==1" class="uni-input-wrapper">{{item.value}}</view>
				<view v-else class="uni-input-wrapper">
					<input class="uni-input" maxlength="20" :placeholder="msg+item.label" v-model="item.value" />
				</view>
				<label class="color-red"
					v-if="errormsgs[item.name[0]+'.'+item.name[1]]!=null">{{errormsgs[item.name[0]+'.'+item.name[1]]}}</label>
			</view> -->
				<block v-else>
					<uni-forms-item :name="item.name[1]" :required="Required(item)" :label="item.label">
						<uni-easyinput type="text" :inputBorder="true" v-model="formData[item.name[1]]"
							class="uni-input-border" :placeholder="msg+item.label"></uni-easyinput>
					</uni-forms-item>
					<label class="color-red"
						v-if="errormsgs[item.name[0]+'.'+item.name[1]]!=null">{{errormsgs[item.name[0]+'.'+item.name[1]]}}</label>
				</block>
			</view>
			<view class="uni-padding-wrap uni-common-mt" v-if="readonly!=1">
				<view class="button-sp-area">
					<button type="default" @click="onFinish()">
						<slot name="Finish">提交</slot>
					</button>
				</view>
			</view>
			<view class="uni-padding-wrap uni-common-mt" v-if="readonly!=1">
				<view class="button-sp-area">
					<button type="default" @click="onReset()">
						<slot name="Reset">重置</slot>
					</button>
				</view>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		Reset
	} from '../../pages/schoollist/controller/index.js'
	import ajax from '../../myservice/ajaxservice.js'
	import {
		WTM_ValueType
	} from '../../common/wtm/declare.js'

	export default {
		data() {
			return {
				formData: {},
				rules: {},
				msg: "请输入",
				errormsgs: [],
				pageEntity: [],
			}
		},
		props: {
			// 检测类型 + 其他验证
			Entity: {
				type: Array,
				default: () => {
					return [];
				},
				required: true,
			},
			ID: {
				type: Number,
				default: 0,
				//required: false,
			},
			readonly: {
				type: Number,
				default: 0,
				//required: false,
			},
			isBatchUpdate: {
				type: Number,
				default: 0,
				//required: false,
			}
		},
		inject: ["Pagination"],
		methods: {
			onFinish() {
				var that = this;
				console.log(JSON.stringify(that.formData))
				console.log(JSON.stringify(that.rules))
				that.$refs['form']
					.submit()
					.then(res => {
						var data = {};
						that.Entity.map(item => {
							if (that.formData[item.name[1]] != null && item.type != WTM_ValueType.select) {
								item.value = that.formData[item.name[1]];
							}
							data[item.name[1]] = item.value;
						})
						console.log(JSON.stringify(that.Entity))
						if (that.ID > 0) { //大于0代表修改
							data.ID = that.ID;
							that.update(data); //修改
							return;
						} else if (that.isBatchUpdate == 1) {
							//批量修改
							that.batchUpdate(data);
							return;
						}
						that.insert(data); //添加
					})
					.catch(errors => {
						console.error('验证失败：', errors)
						uni.showToast({
							title: errors[0].errorMessage,
							icon: 'none'
						});
					})
			},
			batchUpdate(data) { //批量修改 batchupdate
				var that = this;
				if (that.Pagination.Ids.length < 1) {
					uni.showToast({
						title: '没有要修改的项',
						icon: 'none'
					});
					return;
				}
				ajax.webRequest({
					url: Reset.batchupdate,
					method: 'POST',
					data: {
						LinkedVM: data,
						Ids: that.Pagination.Ids
					}
				}).then(res => {
					if (res.statusCode == 200) {
						uni.showToast({
							title: '修改成功',
							icon: 'none'
						});
						uni.navigateBack({
							delta: 1
						});
					} else {
						that.errormsgs = res.data.Form;
						if (res.data.Message.length > 0) {
							uni.showToast({
								title: res.data.Message[0]
							});
						}
					}
				})
			},
			update(data) { //修改
				ajax.webRequest({
					url: Reset.update,
					method: 'PUT',
					data: {
						Entity: data
					}
				}).then(res => {
					if (res.statusCode == 200) {
						uni.showToast({
							title: '修改成功',
							icon: 'none'
						});
						uni.navigateBack({
							delta: 1
						});
					} else {
						that.errormsgs = res.data.Form;
						if (res.data.Message.length > 0) {
							uni.showToast({
								title: res.data.Message[0]
							});
						}
					}
				})
			},
			insert(data) { //添加
				var that = this;
				ajax.webRequest({
					url: Reset.insert,
					method: 'POST',
					data: {
						Entity: data
					}
				}).then(res => {

					if (res.statusCode == 200) {
						uni.showToast({
							title: '添加成功',
							icon: 'none'
						});
					} else {
						that.errormsgs = res.data.Form;
						if (res.data.Message.length > 0) {
							uni.showToast({
								title: res.data.Message[0]
							});
						}
					}
				})
			},
			bindPickerChange(e, index) { //下拉框
				this.Entity[index].index = e.detail.value;
				this.Entity[index].value = this.Entity[index].request[e.detail.value].Value;
			},
			onReset() {
				var that = this;
				that.Entity.forEach((item, index) => {
					// if (item.type == null || item.type == "text") {
					// 	item.value = "";
					// } else if (item.type = "select") {
					// 	item.value = item.request[0].Value;
					// }
					item.value = that.pageEntity[index].value;
					if (item.type == WTM_ValueType.select) {
						item.index = that.pageEntity[index].index;
					}
				})
				console.log(JSON.stringify(that.pageEntity))
			},
			Required(e) {
				var b = false;
				if (e.rules != null) {
					e.rules.forEach(item => {
						if (item.required) {
							console.log(item.required);
							b = true;
						}
					})
				}
				return b;
			}
		},
		mounted() {
			var that = this;
			that.pageEntity = JSON.parse(JSON.stringify(that.Entity));

			if (that.ID > 0) {
				console.log(Reset.details.replace("{ID}", that.ID), )
				ajax.webRequest({
					url: Reset.details.replace("{ID}", that.ID),
					method: 'GET'
				}).then(res => {
					console.log(JSON.stringify(res));
					that.Entity.map(item => {
						if (res.data.Entity[item.name[1]] != null) {
							item.value = res.data.Entity[item.name[1]];
						}
					})
					var dd = {}; //存formdata数据
					var da = {}; //存rules数据
					that.Entity.forEach((item, index) => {
						console.log(item.type+"____"+WTM_ValueType.select)
						if (item.type != WTM_ValueType.select) { 
							dd[item.name[1]] = item.value;
						}
						if (item.rules != null) {
							da[item.name[1]] = {
								rules: item.rules
							}
						}
					})
					that.formData = dd;
					that.rules = da;
				});
			} else {
				var dd = {}; //存formdata数据
				var da = {}; //存rules数据
				that.Entity.forEach((item, index) => {
					if (item.type != WTM_ValueType.select) {
						dd[item.name[1]] = item.value;
					}
					if (item.rules != null) {
						da[item.name[1]] = {
							rules: item.rules
						}
					}
				})
				that.formData = dd;
				that.rules = da;
			}


		}
	}
</script>

<style>
	.color-red {
		color: red;
	}
</style>
